package com.openbravo.pos.panels;

import com.openbravo.basic.BasicException;
import com.openbravo.data.user.ListProvider;
import com.openbravo.data.user.ListProviderCreator;
import com.openbravo.editor.JEditorKeys;
import com.openbravo.pos.forms.AppLocal;
import com.openbravo.pos.forms.DataLogicSales;
import com.openbravo.pos.ticket.ProductFilterSales;
import com.openbravo.pos.ticket.ProductInfoExt;
import com.openbravo.pos.ticket.ProductRenderer;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import javax.swing.AbstractListModel;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.ListModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class JProductFinder extends JDialog
{
  private ProductInfoExt m_ReturnProduct;
  private ListProvider lpr;
  public static final int PRODUCT_ALL = 0;
  public static final int PRODUCT_NORMAL = 1;
  public static final int PRODUCT_AUXILIAR = 2;
  private JButton jButton3;
  private JList jListProducts;
  private JPanel jPanel1;
  private JPanel jPanel2;
  private JPanel jPanel3;
  private JPanel jPanel4;
  private JPanel jPanel5;
  private JScrollPane jScrollPane1;
  private JButton jcmdCancel;
  private JButton jcmdOK;
  private JEditorKeys m_jKeys;
  private JPanel m_jProductSelect;

  private JProductFinder(Frame paramFrame, boolean paramBoolean)
  {
    super(paramFrame, paramBoolean);
  }

  private JProductFinder(Dialog paramDialog, boolean paramBoolean)
  {
    super(paramDialog, paramBoolean);
  }

  private ProductInfoExt init(DataLogicSales paramDataLogicSales, int paramInt)
  {
    initComponents();
    this.jScrollPane1.getVerticalScrollBar().setPreferredSize(new Dimension(35, 35));
    ProductFilterSales localProductFilterSales = new ProductFilterSales(paramDataLogicSales, this.m_jKeys);
    localProductFilterSales.activate();
    this.m_jProductSelect.add(localProductFilterSales, "Center");
    switch (paramInt)
    {
    case 1:
      this.lpr = new ListProviderCreator(paramDataLogicSales.getProductListNormal(), localProductFilterSales);
      break;
    case 2:
      this.lpr = new ListProviderCreator(paramDataLogicSales.getProductListAuxiliar(), localProductFilterSales);
      break;
    default:
      this.lpr = new ListProviderCreator(paramDataLogicSales.getProductList(), localProductFilterSales);
    }
    this.jListProducts.setCellRenderer(new ProductRenderer());
    getRootPane().setDefaultButton(this.jcmdOK);
    this.m_ReturnProduct = null;
    setVisible(true);
    return this.m_ReturnProduct;
  }

  private static Window getWindow(Component paramComponent)
  {
    if (paramComponent == null)
      return new JFrame();
    if (((paramComponent instanceof Frame)) || ((paramComponent instanceof Dialog)))
      return (Window)paramComponent;
    return getWindow(paramComponent.getParent());
  }

  public static ProductInfoExt showMessage(Component paramComponent, DataLogicSales paramDataLogicSales)
  {
    return showMessage(paramComponent, paramDataLogicSales, 0);
  }

  public static ProductInfoExt showMessage(Component paramComponent, DataLogicSales paramDataLogicSales, int paramInt)
  {
    Window localWindow = getWindow(paramComponent);
    JProductFinder localJProductFinder;
    if ((localWindow instanceof Frame))
      localJProductFinder = new JProductFinder((Frame)localWindow, true);
    else
      localJProductFinder = new JProductFinder((Dialog)localWindow, true);
    return localJProductFinder.init(paramDataLogicSales, paramInt);
  }

  private void initComponents()
  {
    this.jPanel4 = new JPanel();
    this.m_jKeys = new JEditorKeys();
    this.jPanel2 = new JPanel();
    this.m_jProductSelect = new JPanel();
    this.jPanel3 = new JPanel();
    this.jButton3 = new JButton();
    this.jPanel5 = new JPanel();
    this.jScrollPane1 = new JScrollPane();
    this.jListProducts = new JList();
    this.jPanel1 = new JPanel();
    this.jcmdOK = new JButton();
    this.jcmdCancel = new JButton();
    setDefaultCloseOperation(2);
    setTitle(AppLocal.getIntString("form.productslist"));
    this.jPanel4.setLayout(new BorderLayout());
    this.jPanel4.add(this.m_jKeys, "North");
    getContentPane().add(this.jPanel4, "After");
    this.jPanel2.setLayout(new BorderLayout());
    this.m_jProductSelect.setLayout(new BorderLayout());
    this.jButton3.setIcon(new ImageIcon(getClass().getResource("/com/openbravo/images/launch.png")));
    this.jButton3.setText(AppLocal.getIntString("button.executefilter"));
    this.jButton3.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent paramActionEvent)
      {
        JProductFinder.this.jButton3ActionPerformed(paramActionEvent);
      }
    });
    this.jPanel3.add(this.jButton3);
    this.m_jProductSelect.add(this.jPanel3, "South");
    this.jPanel2.add(this.m_jProductSelect, "North");
    this.jPanel5.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
    this.jPanel5.setLayout(new BorderLayout());
    this.jScrollPane1.setVerticalScrollBarPolicy(22);
    this.jListProducts.setSelectionMode(0);
    this.jListProducts.addListSelectionListener(new ListSelectionListener()
    {
      public void valueChanged(ListSelectionEvent paramListSelectionEvent)
      {
        JProductFinder.this.jListProductsValueChanged(paramListSelectionEvent);
      }
    });
    this.jListProducts.addMouseListener(new MouseAdapter()
    {
      public void mouseClicked(MouseEvent paramMouseEvent)
      {
        JProductFinder.this.jListProductsMouseClicked(paramMouseEvent);
      }
    });
    this.jScrollPane1.setViewportView(this.jListProducts);
    this.jPanel5.add(this.jScrollPane1, "Center");
    this.jPanel2.add(this.jPanel5, "Center");
    this.jPanel1.setLayout(new FlowLayout(2));
    this.jcmdOK.setIcon(new ImageIcon(getClass().getResource("/com/openbravo/images/button_ok.png")));
    this.jcmdOK.setText(AppLocal.getIntString("Button.OK"));
    this.jcmdOK.setEnabled(false);
    this.jcmdOK.setMargin(new Insets(8, 16, 8, 16));
    this.jcmdOK.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent paramActionEvent)
      {
        JProductFinder.this.jcmdOKActionPerformed(paramActionEvent);
      }
    });
    this.jPanel1.add(this.jcmdOK);
    this.jcmdCancel.setIcon(new ImageIcon(getClass().getResource("/com/openbravo/images/button_cancel.png")));
    this.jcmdCancel.setText(AppLocal.getIntString("Button.Cancel"));
    this.jcmdCancel.setMargin(new Insets(8, 16, 8, 16));
    this.jcmdCancel.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent paramActionEvent)
      {
        JProductFinder.this.jcmdCancelActionPerformed(paramActionEvent);
      }
    });
    this.jPanel1.add(this.jcmdCancel);
    this.jPanel2.add(this.jPanel1, "South");
    getContentPane().add(this.jPanel2, "Center");
    Dimension localDimension = Toolkit.getDefaultToolkit().getScreenSize();
    setBounds((localDimension.width - 665) / 2, (localDimension.height - 565) / 2, 665, 565);
  }

  private void jListProductsMouseClicked(MouseEvent paramMouseEvent)
  {
    if (paramMouseEvent.getClickCount() == 2)
    {
      this.m_ReturnProduct = ((ProductInfoExt)this.jListProducts.getSelectedValue());
      dispose();
    }
  }

  private void jcmdOKActionPerformed(ActionEvent paramActionEvent)
  {
    this.m_ReturnProduct = ((ProductInfoExt)this.jListProducts.getSelectedValue());
    dispose();
  }

  private void jcmdCancelActionPerformed(ActionEvent paramActionEvent)
  {
    dispose();
  }

  private void jListProductsValueChanged(ListSelectionEvent paramListSelectionEvent)
  {
    this.jcmdOK.setEnabled(this.jListProducts.getSelectedValue() != null);
  }

  private void jButton3ActionPerformed(ActionEvent paramActionEvent)
  {
    try
    {
      this.jListProducts.setModel(new MyListData(this.lpr.loadData()));
      if (this.jListProducts.getModel().getSize() > 0)
        this.jListProducts.setSelectedIndex(0);
    }
    catch (BasicException localBasicException)
    {
      localBasicException.printStackTrace();
    }
  }

  private static class MyListData extends AbstractListModel
  {
    private List m_data;

    public MyListData(List paramList)
    {
      this.m_data = paramList;
    }

    public Object getElementAt(int paramInt)
    {
      return this.m_data.get(paramInt);
    }

    public int getSize()
    {
      return this.m_data.size();
    }
  }
}

/* Location:           D:\PowerFulPOS-2.30.2\openbravopos.jar
 * Qualified Name:     com.openbravo.pos.panels.JProductFinder
 * JD-Core Version:    0.6.0
 */